<template>
  <div id="app" class="skEyePush">
    <j-modal
      :title="title"
      :width="width"
      :visible="visible"
      switchFullscreen
      @ok="handleOk"
      :confirmLoading="confirmLoading"
      :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <a-tree
        v-model="checkedKeys"
        checkable
        :expanded-keys="expandedKeys"
        :auto-expand-parent="autoExpandParent"
        :selected-keys="selectedKeys"
        :tree-data="treeData"
        @expand="onExpand"
        :replaceFields="replaceFields"
      />
    </j-modal>
  </div>
</template>
<script>
import { getAction } from '@/api/manage.js'
import Vue from 'vue'
import { USER_INFO } from "@/store/mutation-types"
export default {
  name: 'skEyePush',
  data() {
    return {
      title: '选择推送的幼儿',
      width: 500,
      visible: false,
      disableSubmit: false,
      confirmLoading: false,
      expandedKeys: [],
      autoExpandParent: true,
      checkedKeys: [],
      selectedKeys: [],
      treeData:[],
      replaceFields:{
        children:"studentList",
        title:"name",
        key:"id"
      }
    }
  },
  components: {},

  filters: {},

  computed: {},

  created() {
  },
  
  activated() {},
  
  mounted() {},
  
  methods: {
    init() {
      this.getStudentList();
      this.checkedKeys = []
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys);
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    getStudentList(){
      const url = "/management/skStudent/getClassStudents"
      const params = {
        classIds:Vue.ls.get(USER_INFO).classId
      }
      getAction(url,params).then((res)=>{
        if(res.success){
          this.treeData = []
          res.result.forEach(item => {
            this.treeData.push({
              id:item.classId,
              name:item.className,
              studentList:item.studentList
            })
          });
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
      })
    },
    handleOk() {
      this.checkedKeys.forEach((item,index) => {
        this.treeData.forEach(el => {
          if (item == el.id) {
            this.checkedKeys.splice(index,1)
          }
        });
      });
      console.log(this.checkedKeys);
      if (this.checkedKeys.length != 0) {
        const url = '/health/skVisionScreening/pushMessage'
        const params = {
          studentIds:this.checkedKeys.join(",")
        }
        this.confirmLoading = true
        getAction(url,params)
        .then(res => {
          if (res.success) {
            this.$notification["warning"]({
              message: '推送结果通知',
              description:res.message,
            });
            this.close()
          } else {
            this.$message.warning(res.message)
          }
          this.confirmLoading = false
        })
        .catch(error => {
          this.confirmLoading = false
          console.error(error)
        })
      }else{
        this.$message.warning("请选择幼儿！")
      }
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
